package com.zxy.ziems.server.classify.controller;

import com.zxy.btp.common.response.JsonResponse;
import com.zxy.btp.common.response.PageResult;
import com.zxy.ziems.server.classify.pojo.dto.ClassifyDevicePropertyDTO;
import com.zxy.ziems.server.classify.pojo.dto.ClassifyDto;
import com.zxy.ziems.server.classify.pojo.param.ClassifyDevicePropertyPageParam;
import com.zxy.ziems.server.classify.pojo.param.ClassifyIds;
import com.zxy.ziems.server.classify.pojo.vo.ClassifyDevicePropertyVO;
import com.zxy.ziems.server.classify.pojo.vo.ClassifyTreeNode;
import com.zxy.ziems.server.classify.pojo.vo.ClassifyVO;
import com.zxy.ziems.server.classify.service.ClassifyDevicePropertyService;
import com.zxy.ziems.server.classify.service.ClassifyService;
import com.zxy.ziems.server.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数据分类
 *
 * @ClassName ClassifyController
 * @Author 邓伟
 * @Date 2023/11/27 13:39
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/classify")
public class ClassifyController {

    @Autowired
    private ClassifyService classifyService;

    /**
     * 新增/修改分类
     *
     * @param classifyDto
     * @return
     */
    @PostMapping("/addOrUpdate")
    public JsonResponse addOrUpdate(@RequestBody @Validated ClassifyDto classifyDto) {
        classifyService.addOrUpdate(classifyDto);
        return JsonResponse.SUCCESS();
    }

    /**
     * 批量新增数据分类
     *
     * @param classifyDto
     * @return
     */
    @PostMapping("/addClassifys")
    public JsonResponse addClassifys(@RequestBody @Validated ClassifyDto classifyDto) {
        // 要给个初始的sort值
        // classifyName随便给个值，集合里面才是会给系统创建的
        int sortNum = classifyDto.getSort();
        for (String name : classifyDto.getClassifyNameList()) {
            classifyDto.setSort(sortNum+=2);
            classifyDto.setClassifyName(name);
            classifyService.addOrUpdate(classifyDto);
        }
        return JsonResponse.SUCCESS();
    }

    /**
     * 删除分类
     *
     * @param classifyId
     * @return
     */
    @GetMapping("/delete")
    public JsonResponse delete(@RequestParam(value = "classifyId") String classifyId) {
        classifyService.delete(classifyId);
        return JsonResponse.SUCCESS();
    }

    /**
     * 分类明细
     *
     * @param classifyId
     * @return
     */
    @GetMapping("/detail")
    public JsonResponse<ClassifyVO> detail(@RequestParam(value = "classifyId") String classifyId) {
        ClassifyVO classify = classifyService.detailById(classifyId);
        return JsonResponse.SUCCESS(classify);
    }

    /**
     * 分类树
     *
     * @param dataObtainType
     * @param type           不给默认是只查询分类，为1是额外查询分类下的设备
     * @return
     */
    @GetMapping("/tree")
    public JsonResponse<List<ClassifyTreeNode>> tree(@RequestParam(value = "dataObtainType", required = false) Integer dataObtainType, @RequestParam(value = "type", required = false) Integer type) {
        List<ClassifyTreeNode> treeNodeList = classifyService.getTree(dataObtainType, type);
        return JsonResponse.SUCCESS(treeNodeList);
    }


    /**
     * 分类树（通过父ID获取下面的树）
     *
     * @param dataObtainType
     * @param type             1:包含设备；默认分类
     * @param parentClassifyId 上级ClassifyId
     * @return
     */
    @GetMapping("/tree/parent")
    public JsonResponse<List<ClassifyTreeNode>> treeByParentClassifyId(@RequestParam(value = "dataObtainType", required = false) Integer dataObtainType,
                                                                       @RequestParam(value = "type", required = false) Integer type,
                                                                       @RequestParam(value = "parentClassifyId") String parentClassifyId) {
        AssertUtil.isTrue(StringUtils.isNotBlank(parentClassifyId), "parentClassifyId不可为空");
        return JsonResponse.SUCCESS(classifyService.treeByParentClassifyId(dataObtainType, type, parentClassifyId));
    }

    /**
     * 通过父id获取分类数据
     *
     * @param parentClassifyId 父ClassifyId
     * @return
     */
    @GetMapping("/getByParent")
    public JsonResponse<List<ClassifyVO>> getByParentClassifyId(@RequestParam(value = "parentClassifyId") String parentClassifyId) {
        List<ClassifyVO> treeNodeList = classifyService.getByParentClassifyId(parentClassifyId);
        return JsonResponse.SUCCESS(treeNodeList);
    }

    /**
     * 通过父ids获取分类数据
     *
     * @param classifyIds 父ClassifyIds
     * @return
     */
    @PostMapping("/getByParents")
    public List<ClassifyVO> getByParentClassifyIds(@RequestBody ClassifyIds classifyIds) {
        if (classifyIds == null || classifyIds.getClassifyIds() == null || classifyIds.getClassifyIds().isEmpty()) {
            return new ArrayList<>();
        }
        List<ClassifyVO> treeNodeList = classifyService.getByParentClassifyIds(classifyIds.getClassifyIds());
        return treeNodeList;
    }


    /**
     * 通过父id获取分类数据（字典配置）
     *
     * @param dictCode 字典code
     * @param itemName 字典属性名
     * @return
     */
    @GetMapping("/getByParentByDict")
    public JsonResponse<List<ClassifyVO>> getByParentByDict(@RequestParam(value = "dictCode") String dictCode, @RequestParam(value = "itemName") String itemName) {
        List<ClassifyVO> treeNodeList = classifyService.getByParentByDict(dictCode, itemName);
        return JsonResponse.SUCCESS(treeNodeList);
    }


    /**
     * 获取分类下设备
     *
     * @param classifyId
     * @return
     */
    @GetMapping("/deviceList")
    public JsonResponse<List<ClassifyTreeNode>> deviceListByClassId(@RequestParam(value = "classifyId") String classifyId) {
        List<ClassifyTreeNode> treeNodeList = classifyService.deviceListByClassId(classifyId);
        return JsonResponse.SUCCESS(treeNodeList);
    }

    /**
     * 查询当前分类下所有设备
     *
     * @param param
     * @return
     */
    @GetMapping("/device/page")
    public PageResult<ClassifyDevicePropertyVO> devicePageList(@Validated ClassifyDevicePropertyPageParam param) {
        return classifyService.pageListBy(param);
    }

    /**
     * 删除分类下的设备
     *
     * @param dto
     */
    @PostMapping("device/delete")
    public void deviceDelete(@RequestBody @Validated(ClassifyDevicePropertyDTO.Delete.class) ClassifyDevicePropertyDTO dto) {
        classifyDevicePropertyService.deleteById(dto.getId());
    }

    /**
     * 分类下的添加设备
     *
     * @param dto
     */
    @PostMapping("device/add")
    public void deviceAdd(@RequestBody @Validated(ClassifyDevicePropertyDTO.Add.class) ClassifyDevicePropertyDTO dto) {
        classifyService.saveClassifyDevice(dto);
    }

    /**
     * 分类下的添加设备集
     *
     * @param dto
     */
    @PostMapping("device/adds")
    public void deviceAdds(@RequestBody @Validated(ClassifyDevicePropertyDTO.AddList.class) ClassifyDevicePropertyDTO dto) {
        List<String> deviceIds = dto.getDeviceIds();
        for (String deviceId : deviceIds) {
            dto.setDeviceId(deviceId);
            classifyService.saveClassifyDevice(dto);
        }
    }


    private ClassifyDevicePropertyService classifyDevicePropertyService;

    @Autowired
    public void setClassifyDevicePropertyService(ClassifyDevicePropertyService classifyDevicePropertyService) {
        this.classifyDevicePropertyService = classifyDevicePropertyService;
    }


}
